package cn.soft863.servlet.back;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.soft863.common.util.DateUtil;
import cn.soft863.common.util.PageInfoUtil;
import cn.soft863.users.pojo.AOrdersHistory;
import cn.soft863.users.service.OrdersHistoryService;
import cn.soft863.users.service.OrdersService;
import cn.soft863.users.service.impl.OrdersHistoryServiceImpl;
import cn.soft863.users.service.impl.OrdersServiceImpl;


/**
 * 管理员后台的Servlet
 * 
 * @author 902-B-Book
 *
 */
public class OrdersHistoryBackServlet extends HttpServlet
{
	private OrdersHistoryService orderHistoryService = new OrdersHistoryServiceImpl();
	private DateUtil dateUtil = new DateUtil() ; 
	
	/**
	 * 这个是核心的代码,处理请求的代码
	 */
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 只能处理POST请求的乱码 */
		request.setCharacterEncoding("UTF-8");
		
		/* 添加一个操作符 */
		String method = request.getParameter("method");
		
		System.out.println("---UsersbackServlet----service----------" + method);
		if("ordersHisList".equalsIgnoreCase(method))
		{
			/* 管理员列表页面 */
			ordersHisList(request, response);
		}else if("ordersHisInsert".equalsIgnoreCase(method))
		{
			/* 打开管理员的添加页面呢 */
			ordersHisInsert(request, response);
		}else if("ordersHisInsertSubmit".equalsIgnoreCase(method))
		{
			/* 打开管理员的添加提交页面呢 */
			ordersHisInsertSubmit(request, response);
		}else if("ordersHisUpdate".equalsIgnoreCase(method))
		{
			/* 打开管理员的添加页面呢 */
			ordersHisUpdate(request, response);
		}else if("ordersHisUpdateSubmit".equalsIgnoreCase(method))
		{
			/* 打开管理员的添加提交页面呢 */
			ordersHisUpdateSubmit(request, response);
		}
	}

	/**
	 * 管理员更新提交
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void ordersHisUpdateSubmit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		System.out.println("=======orderUpdateSubmit====");
		/* 接收参数
		 * 不管是post还是get,提交的字符串都是以此种格式显示
		 * 键=值&键=值&键=值&键=值&键=值&键=值&键=值&
		 * 参数是键
		 * method=orderUpdate&aid=4
		 * 
		 * 获取Id */
		String id = request.getParameter("aid");
		
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		AOrdersHistory order = this.orderHistoryService.findOneorderHistoryService(condMap);
		
		/* 接收参数 */
		String usersId = request.getParameter("usersId");
		String ordersId = request.getParameter("ordersId");
		String adminsId = request.getParameter("adminsId");
		String content = request.getParameter("content");
		String status = request.getParameter("status");
		/* 处理参数 */
		int usersIdInt =	Integer.parseInt(usersId) ;
		int ordersIdInt = Integer.parseInt(ordersId) ;
		int adminsIdInt = Integer.parseInt(adminsId) ;
		byte statusByte = Byte.valueOf(status);
		

		/* 赋值 */	
		/* 页面上不需要赋值的,后台直接填写 */
		order.setOrdersId(ordersIdInt);
		order.setUsersId(usersIdInt);
		order.setAdminsId(adminsIdInt);
		order.setContent(content);
		order.setStatus(statusByte);
		order.setUpdateTime(new Date());


		
		/* 调用Service的方法,存储结果
		 * Test类,真的就可以将数据存储到数据库了
		 *  */
		int res = this.orderHistoryService.updateOneorderHistoryService(order);
		System.out.println(res + "------------------");
		/* 存储结果 */
		String info = "" ; 
		if(res > 0 )
		{
			/* Sucess */
			info = "成功,id:" + order.getId() ; 
		}else
		{
			info = "失败" ;
		}
		request.setAttribute("info", info);
		
		
		String url2 = "/WEB-INF/jsp/back/ordersHistoryUpdate.jsp" ; 
		request.getRequestDispatcher(url2).forward(request, response);
	}
	
	/**
	 * 管理员更新打开页面
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void ordersHisUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		System.out.println("=======orderUpdate====");
		
		/* 接收参数
		 * 不管是post还是get,提交的字符串都是以此种格式显示
		 * 键=值&键=值&键=值&键=值&键=值&键=值&键=值&
		 * 参数是键
		 * method=orderUpdate&aid=4
		 * 
		 * 获取Id */
		String id = request.getParameter("aid");
		
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		AOrdersHistory order = this.orderHistoryService.findOneorderHistoryService(condMap);
		/* 将结果存储到四大作用域中 */
		request.setAttribute("order", order);
		
		String url = "/WEB-INF/jsp/back/ordersHistoryUpdate.jsp" ; 
		request.getRequestDispatcher(url).forward(request, response);
	}

	/**
	 * 打开管理员的添加提交页面呢
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void ordersHisInsertSubmit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		System.out.println("======orderInsertSubmit=======");
		
		/* 如何接收页面的信息呢? */
		String usersId = request.getParameter("usersId");
		String ordersId = request.getParameter("ordersId");
		String adminsId = request.getParameter("adminsId");
		String content = request.getParameter("content");
		String status = request.getParameter("status");
		/* 处理参数 */
		int usersIdInt =	Integer.parseInt(usersId) ;
		int ordersIdInt = Integer.parseInt(ordersId) ;
		int adminsIdInt = Integer.parseInt(adminsId) ;
		byte statusByte = Byte.valueOf(status);
		
		AOrdersHistory order = new AOrdersHistory() ; 
		/* 赋值 */	
		/* 页面上不需要赋值的,后台直接填写 */
		order.setOrdersId(ordersIdInt);
		order.setUsersId(usersIdInt);
		order.setAdminsId(adminsIdInt);
		order.setContent(content);
		order.setStatus(statusByte);
		order.setCreateTime(new Date());
		order.setUpdateTime(new Date());
		order.setPubTime(new Date());

		
		/* 调用Service的方法,存储结果
		 * Test类,真的就可以将数据存储到数据库了
		 *  */
		int res = this.orderHistoryService.saveOneorderHistoryService(order);
		System.out.println(res + "------------------");
		/* 存储结果 */
		String info = "" ; 
		if(res > 0 )
		{
			/* Sucess */
			info = "成功,id:" + order.getId() ; 
		}else
		{
			info = "失败" ;
		}
		request.setAttribute("info", info);
		/* 服务器端跳转到管理员添加的jsp上面 */
		String url2 = "/WEB-INF/jsp/back/ordersHistoryInsert.jsp" ; 
		request.getRequestDispatcher(url2).forward(request, response);
	}
	
	/**
	 * 打开管理员的添加页面呢
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void ordersHisInsert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		System.out.println("======orderInsert=======");
		/* 服务器端跳转到管理员添加的jsp上面 */
		String url = "/WEB-INF/jsp/back/ordersHistoryInsert.jsp" ; 
		request.getRequestDispatcher(url).forward(request, response);
	}

	/**
	 * 管理员列表页面
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void ordersHisList(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 要想翻页,得让当前页发生变化才行 
		 * 需要从页面上获取当前页和每页多少条
		 * 
		 * 不管是post还是get,是表单还是url
		 * 所传递的参数对都是键=值&键=传&键=传&键=传&键=传&键=传
		 * 
		 * String 值 = request.getParameter("键");
		 * */
		String currentPage = request.getParameter("currentPage");
		String pageSize = request.getParameter("pageSize");
		
		PageInfoUtil pageInfoUtil = new PageInfoUtil() ; 
		/* currentPage如何木有传或者传入的不是整数,咋办?
		 * 转换数据会报错,咋办? */
		try
		{
			pageInfoUtil.setCurrentPage(Integer.valueOf(currentPage));
			pageInfoUtil.setPageSize(Integer.valueOf(pageSize));
		} catch (NumberFormatException e)
		{
		}
		
		
		/* 传搜索要用的
		 * 
		 * 凡是Servlet需要的东西,需要的参数,都问jsp要,都从request里面取
		 *  */
		String keyword = request.getParameter("keyword");
		String status = request.getParameter("status");
		String st = request.getParameter("st");
		String ed = request.getParameter("ed");
		
		if(keyword == null)
		{
			keyword = ""; 
		}
		/* 取掉前后空格 */
		keyword = keyword.trim() ;
		if(status == null)
		{
			status = "" ; 
		}
		if(st == null)
		{
			st = "" ; 
		}
		if(ed == null)
		{
			ed = "" ; 
		}
		
		Date stDate = null ; 
		Date edDate = null ; 
		
		/* 如何将字符串的日期时间变成Date对象呢? */
		if(!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed))
		{
			stDate = dateUtil.strToDateTime(st);
			edDate = dateUtil.strToDateTime(ed);
		}
		
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 按照关键字查询 */
		condMap.put("keyword", keyword);
		/* 按照状态查询 */
		condMap.put("status", status);
		condMap.put("stDate", stDate);
		condMap.put("edDate", edDate);
		
		/**
		 * 终于将查询的结果放到了集合中
		 */
		List<AOrdersHistory> ordersHisList = this.orderHistoryService.findCondListorderHistoryService(pageInfoUtil,condMap);
		/* 将Service调用方法的返回值放到四大作用域中. */
		request.setAttribute("ordersHisList", ordersHisList);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		
		/* 存储搜索条件 */
		request.setAttribute("keyword", keyword);
		request.setAttribute("status", status);
		request.setAttribute("st", st);
		request.setAttribute("ed", ed);
		
		String url = "/WEB-INF/jsp/back/ordersHistoryList.jsp" ; 
		/* 服务器端跳转到jsp上面.
		 * 所有的jsp都放到web-inf/jsp/back中 */
		request.getRequestDispatcher(url).forward(request, response);
	}
}
